﻿// -------------------------------------------------------------------------
//    @FileName         :    NFCityPlayerModule.cpp
//    @Author           :    gaoyi
//    @Date             :    2025-02-27
//    @Email			:    445267987@qq.com
//    @Module           :    NFCityPlayerModule
//
// -------------------------------------------------------------------------

#include "NFCityPlayerModule.h"

#include "NFCityMapMgr.h"

NFCityPlayerModule::NFCityPlayerModule(NFIPluginManager* p) : NFMMODynamicModule(p)
{
}

NFCityPlayerModule::~NFCityPlayerModule()
{

}

int NFCityPlayerModule::Awake()
{
	NFServerConfig* pConfig = FindModule<NFIConfigModule>()->GetAppConfig(NF_ST_CITY_SERVER);
	CHECK_NULL(0, pConfig);

    RegisterAppTask(NF_ST_CITY_SERVER, APP_INIT_DESC_STORE_LOAD, GetServerName(NF_ST_CITY_SERVER) + " Load Desc Store", APP_INIT_TASK_GROUP_SERVER_LOAD_DESC_STORE);
	return 0;
}

int NFCityPlayerModule::Init()
{
    uint64_t serverLinkId = FindModule<NFIMessageModule>()->GetServerLinkId(NF_ST_CITY_SERVER);
    CHECK_EXPR_ASSERT(serverLinkId > 0, -1, "logic server link id <= 0");

    FindModule<NFIMessageModule>()->AddOtherCallBack(NF_ST_CITY_SERVER, serverLinkId, this, &NFCityPlayerModule::OnHandleServerOtherMessage);
    return 0;
}

int NFCityPlayerModule::Tick()
{
	return NFIModule::Tick();
}

int NFCityPlayerModule::AfterObjFromDBLoaded()
{
    return NFCityMapMgr::Instance()->Init();
}

int NFCityPlayerModule::OnHandleClientMessage(uint64_t unLinkId, NFDataPackage& packet)
{
	if (!m_pObjPluginManager->IsInited(NF_ST_CITY_SERVER))
	{
		NFLogError(NF_LOG_DEFAULT, packet.nParam1, "City Server not inited, drop client msg:{}", packet.ToString());
		return -1;
	}

	if (m_pObjPluginManager->IsServerStopping())
	{
		NFLogError(NF_LOG_DEFAULT, packet.nParam1, "City Server is Stopping, drop client msg:{}", packet.ToString());
		return -1;
	}


	switch (packet.nMsgId)
	{
	default:
		{
			NFLogError(NF_LOG_DEFAULT, 0, "Client MsgId:{} Register, But Not Handle, Package:{}", packet.nMsgId, packet.ToString());
			break;
		}
	}
	return 0;
}

int NFCityPlayerModule::OnHandleServerMessage(uint64_t unLinkId, NFDataPackage& packet)
{
	if (!m_pObjPluginManager->IsInited(NF_ST_CITY_SERVER))
	{
		NFLogError(NF_LOG_DEFAULT, packet.nParam1, "City Server not inited, drop client msg:{}", packet.ToString());
		return -1;
	}

	if (m_pObjPluginManager->IsServerStopping())
	{
		NFLogError(NF_LOG_DEFAULT, packet.nParam1, "City Server is Stopping, drop client msg:{}", packet.ToString());
		return -1;
	}

	switch (packet.nMsgId)
	{
	default:
		{
			NFLogError(NF_LOG_DEFAULT, 0, "Server MsgId:{} Register, But Not Handle, Package:{}", packet.nMsgId, packet.ToString());
			break;
		}
	}
	return 0;
}

int NFCityPlayerModule::OnHandleServerOtherMessage(uint64_t unLinkId, NFDataPackage& packet)
{
    LOG_ERR(0, -1, "msgId:{} msg:{} not handled!", proto_ff::Proto_CS_CMD_Name(static_cast<proto_ff::Proto_CS_CMD>(packet.nMsgId)), packet.ToString());
    return 0;
}
